package com.tokenplus.thai.assetdetail

import android.annotation.SuppressLint
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.lifecycle.ViewModel
import androidx.lifecycle.ViewModelProvider
import androidx.recyclerview.widget.LinearLayoutManager
import com.tokenplus.thai.adata.user.ReqGetMyTransactions
import com.tokenplus.thai.assetdetail.adapter.AssetDetailAdapter
import com.tokenplus.thai.assetdetail.repo.AssetDetailRepo
import com.tokenplus.thai.aviewmodel.UserViewModel
import com.tokenplus.thai.base.BaseActivity
import com.tokenplus.thai.base.BaseFragment
import com.tokenplus.thai.databinding.FragmentAssetDetailBinding
import com.tokenplus.thai.extend.startToAssetDetailResultActivity

/** asset detail 页 */
class AssetDetailInFragment : BaseFragment() {

    //布局binding
    private lateinit var mFragmentBinding: FragmentAssetDetailBinding

    //适配器
    private lateinit var mAssetDetailAdapter: AssetDetailAdapter

    //user viewModel
    private val mUserViewModel by lazy {
        ViewModelProvider(this)[UserViewModel::class.java]
    }

    override fun onBindingViewModel(): List<ViewModel> {
        val list = mutableListOf<ViewModel>()
        list.add(mUserViewModel)
        return list
    }

    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?,
    ): View {
        mFragmentBinding =
            FragmentAssetDetailBinding.inflate(inflater, container, false)
        return mFragmentBinding.root
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        try {
            //初始化刷新组件
            initRefreshUI()

            //监听返回
            onHandleLiveDataCallback()

            //初始化适配器
            mAssetDetailAdapter = AssetDetailAdapter(mUserViewModel.mAllTransactions)
            mAssetDetailAdapter.setItemClickListener { revDataDetailAsset ->
                AssetDetailRepo.mMyTransactions = revDataDetailAsset
                (requireActivity() as BaseActivity).startToAssetDetailResultActivity()
            }
            mFragmentBinding.listAssetDetail.layoutManager = LinearLayoutManager(requireActivity())
            mFragmentBinding.listAssetDetail.adapter = mAssetDetailAdapter

            //获取交易记录
            getMyTransactions()

        } catch (e: Exception) {
            e.printStackTrace()
        }
    }

    /** 监听live data 返回 */
    @SuppressLint("NotifyDataSetChanged")
    private fun onHandleLiveDataCallback() {
        mUserViewModel.onGetMyTransactionsLiveData.observe(viewLifecycleOwner) {
            stopRefreshState()
            if (it.isSuccess) {
                it.getOrNull()?.let { result ->
                    mUserViewModel.mAllTransactions.clear()
                    mUserViewModel.mAllTransactions.addAll(result)
                    mAssetDetailAdapter.notifyDataSetChanged()

                    if (mUserViewModel.mAllTransactions.size > 0) {
                        mFragmentBinding.emptyLayout.llEmptyList.visibility = View.GONE
                    } else {
                        mFragmentBinding.emptyLayout.llEmptyList.visibility = View.VISIBLE
                    }
                }
            }
        }
    }

    /** 获取交易记录 */
    private fun getMyTransactions() {
        //获取交易记录
        val activity = requireActivity() as AssetDetailActivity
        val coinType = activity.intent.getStringExtra("coinType").toString()
        val reqGetMyTransactions = ReqGetMyTransactions(
            inOrOut = "in",
            coinType = coinType
        )
        reqGetMyTransactions.showLoading = false
        mUserViewModel.getMyTransactions(reqGetMyTransactions)
    }

    /** 初始化下拉刷新 */
    private fun initRefreshUI() {
        connectSwipeRefreshLayout(mFragmentBinding.swipeRefresh) {
            //获取交易记录
            getMyTransactions()
        }
    }
}